import jieba


def readFile(path,encoding):
    return  open(path,mode="rt",encoding=encoding).read()

def cutWord(txt):
    word = jieba.lcut(txt)
    return word

def statisticalWord(List):
    maps={}
    for i in List:
        maps[i]=maps.get(i,0)+1
    return maps

def reject(lis):
    excludes={"，","。","：","“","”"}
    for word in excludes:
        del(lis[word])



def main():
    txt = readFile("test.txt","ANSI")
    List = cutWord(txt)
    maps = statisticalWord(List)
    reject(maps)
    item = list(maps.items())
    item.sort(key=lambda x: x[1],reverse=True)
    out=open("output.txt",mode="w")
    for i in range (5):  
        out.write(str(item[i]))




main()
